﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product;
using ERP_RORZE_DLL.Product.database;

namespace ERP_RORZE_PRODUCT.Product.UserControls
{
    public partial class ctrlBusOderQtyChange : DevExpress.XtraEditors.XtraUserControl
    {
        ClsBusOderDAO clsBusOderDAO = new ClsBusOderDAO();
        DataTable dt;
        ClsBusOderTable b = new ClsBusOderTable();
        ClsMOTable MO = new ClsMOTable();
        ClsMOOptionTable MOO = new ClsMOOptionTable();

        public ctrlBusOderQtyChange()
        {
            InitializeComponent();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (checkFinish.Checked == true)
            {
                MessageBox.Show("Đơn hàng đã kết thúc, không thể thay đổi.");
            }
            else if(txtNewQty.Text == "")
            {
                MessageBox.Show("Chưa nhập NewQty!");
                
            }
            else if (txtNewQty.Text != "")
            {
                try
                {
                    int.Parse(txtNewQty.Text);

                }
                catch
                {
                    MessageBox.Show("New Qty phải là kiểu số nguyên!");
                }
            }
            else if (int.Parse(txtNewQty.Text) > int.Parse(txtOderQty.Text))
            {
                MessageBox.Show("Số lượng mới không được lớn hơn số lượng cũ!");
            }
            else
            {
                

                // Update vào BusOder
                b.Qty = int.Parse(txtNewQty.Text);
                b.Change = "Quantity changed";
                b.ChangeDate = dateUpdateDate.Value;
                clsBusOderDAO.QtyChangeBusOder(b);
            }
        }

        private void ctrlBusOderQtyChange_Load(object sender, EventArgs e)
        {
            dt = clsBusOderDAO.GetAll();
            List<ClsBusOderTable> objs = new List<ClsBusOderTable>();
            foreach (DataRow r in dt.Rows)
            {
                ClsBusOderTable obj = new ClsBusOderTable();
                obj.BOderNo = r["BOderNo"].ToString();
                obj.PlanNo = r["planNo"].ToString();
                obj.MONo = r["MONo"].ToString();

                obj.ChangeDate = (DateTime)r["ChangeDate"];
                obj.Finished = (bool)r["Finished"];
                obj.MOQty = (int)r["MOQty"];
                obj.Qty = (int)r["Qty"];
                objs.Add(obj);
            }
            cbBusOderNo.DataSource = objs;
            cbBusOderNo.DisplayMember = "BOderNo";
        }

        private void cbBusOderNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClsBusOderTable obj = ((ClsBusOderTable)cbBusOderNo.SelectedItem);
            txtMONo.Text = obj.MONo.ToString();
            txtMOQty.Text = obj.MOQty.ToString();
            txtOderQty.Text = obj.Qty.ToString();
            txtPlanNo.Text = obj.PlanNo.ToString();
            checkFinish.Checked = obj.Finished;
            
        }

        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                int.Parse("123456");

            }
            catch
            {
                MessageBox.Show("nhập lại");
            }
        }
    }
}
